package com.controller;

import com.entity.NewsEntity;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.service.NewsService;
import com.utils.PageUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
public class NewsControllerTest {

    private MockMvc mockMvc;

    @Mock
    private NewsService newsService;

    @InjectMocks
    private NewsController newsController;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mockMvc = MockMvcBuilders.standaloneSetup(newsController).build();
    }

    @Test
    public void testAddNews() throws Exception {
        NewsEntity news = new NewsEntity();
        news.setTitle("Test News");
        news.setContent("This is a test news content");

        when(newsService.insert(any(NewsEntity.class))).thenReturn(true);

        ObjectMapper objectMapper = new ObjectMapper();
        String newsJson = objectMapper.writeValueAsString(news);

        mockMvc.perform(post("/news/add")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(newsJson))
                .andExpect(status().isOk());
    }

    @Test
    public void testUpdateNews() throws Exception {
        NewsEntity news = new NewsEntity();
        news.setId(1L);
        news.setTitle("Updated News");
        news.setContent("This is an updated news content");

        when(newsService.updateById(any(NewsEntity.class))).thenReturn(true);

        ObjectMapper objectMapper = new ObjectMapper();
        String newsJson = objectMapper.writeValueAsString(news);

        mockMvc.perform(post("/news/update")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(newsJson))
                .andExpect(status().isOk());
    }

    @Test
    public void testDeleteNews() throws Exception {
        Long[] ids = {1L};
        // 假设 deleteBatchIds 方法返回 boolean 类型
        when(newsService.deleteBatchIds(Arrays.asList(ids))).thenReturn(true);

        ObjectMapper objectMapper = new ObjectMapper();
        String idsJson = objectMapper.writeValueAsString(ids);

        mockMvc.perform(post("/news/delete")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(idsJson))
                .andExpect(status().isOk());
    }

    @Test
    public void testListNews() throws Exception {
        Map<String, Object> params = new HashMap<>();
        NewsEntity news = new NewsEntity();
        PageUtils pageUtils = new PageUtils(null, 0, 10, 1);

        when(newsService.queryPage(anyMap(), any())).thenReturn(pageUtils);

        mockMvc.perform(get("/news/lists")
                        .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk());
    }

    @Test
    public void testQueryNews() throws Exception {
        NewsEntity news = new NewsEntity();
        news.setId(1L);

        when(newsService.selectView(any())).thenReturn(null);

        ObjectMapper objectMapper = new ObjectMapper();
        String newsJson = objectMapper.writeValueAsString(news);

        mockMvc.perform(post("/news/query")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(newsJson))
                .andExpect(status().isOk());
    }

    @Test
    public void testInfoNews() throws Exception {
        Long id = 1L;
        NewsEntity news = new NewsEntity();
        news.setId(id);

        when(newsService.selectById(id)).thenReturn(news);

        mockMvc.perform(get("/news/info/{id}", id)
                        .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk());
    }
}